<?php

namespace app\common\model;

use think\Cache;
use think\Model;
use traits\model\SoftDelete;
use app\common\services\UrlService;

/**
 * 题库
 */
class Question extends Model
{
    protected $table = 'fa_exam_question';

    // 字段类型
    protected $type
        = [
            'options_img' => 'array',
            // 'options_extend' => 'array',
            // 'options_json' => 'array'
        ];

    // 隐藏字段
    // protected $hidden = [
    //     'answer', 'explain'
    // ];

    public function getKindList()
    {
        return [
            'JUDGE'    => '判断题',
            'SINGLE'   => '单选题',
            'MULTI'    => '多选题',
            'FILL'     => '填空题',
            'SHORT'    => '简答题',
            'MATERIAL' => '材料题',
            'WORD'     => '词汇题',
            'CALL'     => '通话题',
        ];
    }

    public function getDifficultyList()
    {
        return ['EASY' => '简单', 'GENERAL' => '普通', 'HARD' => '困难'];
    }

    public function getStatusList()
    {
        return ['NORMAL' => '正常', 'HIDDEN' => '隐藏'];
    }



    public function getOptionsJsonAttr($value)
    {
        if ($value = json_decode($value, true)) {
            $data = [];
            foreach ($value as $key => $row) {
                $arr['key']         = $key;
                $arr['value']       = $row;
                $arr['click_index'] = false;
                array_push($data, $arr);
            }
            return $data;
        }
        return [];
    }

    public function getAnswerAttr($value, $data)
    {
        if (is_array($value)) {
            return $value;
        }
        if (in_array($data['kind'], ['FILL', 'CALL'])) {
            return json_decode($value, true);
        }
        return $value;
    }


    /**
     * 获取试题
     *
     * @return void
     */
    public static function getQuestion($ids){

        $list = self::where('id','IN',$ids)->select();

        foreach($list as &$value){

            if($value['kind'] == 'FILL'){

                $fill  = explode('______',$value['title']);
                $title = [];
                foreach($fill as $va){
                    if(empty($va)){
                        continue;
                    }
                    $title = array_merge($title,[['type' => 1, 'value' => $va],['type' => 0, 'value' => '']]);
                }
                $value['fill_title'] = $title;
            }

            if($value['kind'] == 'FILL'){

            }
        }

        return $list;
    }


    /**
     * 合并材料题子题目
     * @param $questions
     * @return mixed
     */
    public static function mergeMaterialQuestions($questions)
    {
        $questions = collection($questions)->toArray();

        $material_questions = [];
        foreach ($questions as $key => $question) {
            if ($question['kind'] == 'MATERIAL') {
                foreach ($question['material_questions'] as $material_question) {
                    $new_question = $material_question['question'];

                    $new_question['material_id']    = $question['id'];
                    $new_question['material_title'] = $question['title'];
                    $new_question['material_score'] = $question['score'] ?? 1;
                    $new_question['origin_answer']  = $material_question['question']['answer'];
                    $new_question['score']          = $material_question['score'];
                    $new_question['answer']         = $material_question['answer'];

                    // with查询导致的数据格式问题，需要特殊处理
                    if (in_array($new_question['kind'], ['FILL', 'SHORT', 'CALL']) && $new_question['answer'] && is_string($new_question['answer'])) {
                        $new_question['answer'] = json_decode($new_question['answer'], true);
                    }
                    if ($new_question['options_img'] && is_string($new_question['options_img'])) {
                        $new_question['options_img']  = json_decode($new_question['options_img'], true);
                    }
                    if ($new_question['options_json'] && is_string($new_question['options_json'])) {
                        $new_question['options_json'] = json_decode($new_question['options_json'], true);

                        // 特殊格式处理
                        $keys = array_keys($new_question['options_json']);
                        if (isset($keys[0]) && $keys[0] && !is_numeric($keys[0])) {
                            $options_json = [];
                            foreach ($new_question['options_json'] as $option_key => $option_val) {
                                $options_json[] = [
                                    'key'         => $option_key,
                                    'value'       => $option_val,
                                    'click_index' => false,
                                ];
                            }
                            $new_question['options_json'] = $options_json;
                        }
                    }
                    $new_question['show_full'] = false;

                    $material_questions[] = $new_question;

                    // $material_question['question']['material_id']    = $question['id'];
                    // $material_question['question']['material_title'] = $question['title'];
                    // $material_question['question']['material_score'] = $question['score'] ?? 1;
                    // $material_question['question']['origin_answer']  = $material_question['question']['answer'];
                    // $material_question['question']['answer']         = $material_question['answer'];
                    // $material_question['question']['score']          = $material_question['score'];
                    // $material_question['question']['show_full']      = false;
                    // if ($material_question['question']['options_img'] && is_string($material_question['question']['options_img'])) {
                    //     $material_question['question']['options_img'] = json_decode($material_question['question']['options_img'], true);
                    // }
                    // if ($material_question['question']['options_json'] && is_string($material_question['question']['options_json'])) {
                    //     $material_question['question']['options_json'] = json_decode($material_question['question']['options_json'], true);
                    //
                    //     // 特殊格式处理
                    //     $keys = array_keys($material_question['question']['options_json']);
                    //     if (isset($keys[0]) && $keys[0] && !is_numeric($keys[0])) {
                    //         $options_json = [];
                    //         foreach ($material_question['question']['options_json'] as $option_key => $option_val) {
                    //             $options_json[] = [
                    //                 'key'         => $option_key,
                    //                 'value'       => $option_val,
                    //                 'click_index' => false,
                    //             ];
                    //         }
                    //         $material_question['question']['options_json'] = $options_json;
                    //     }
                    // }
                    //
                    // $material_questions[] = $material_question['question'];
                }

                // 删除材料题
                unset($questions[$key]);
                // array_splice($questions, $key, 1);
                // dd($questions);
                // array_slice($questions, $key, 1);
            }
            // dd(collection($material_questions)->toArray());
        }
        // dd(collection($questions)->toArray());
        // dd(collection($material_questions)->toArray());

        if ($material_questions) {
            $questions = array_merge(array_values($questions), $material_questions);
        }

        return $questions;
    }
}
